package com.cbl.v.ssczs.view.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cbl.v.ssczs.R;
import com.cbl.v.ssczs.mode.net.DataCallBack;
import com.cbl.v.ssczs.mode.util.ActivityGroup;
import com.cbl.v.ssczs.mode.util.PreferencesHelper;
import com.cbl.v.ssczs.mode.util.Tools;
import com.cbl.v.ssczs.view.dialog.CommonDialog;
import com.handmark.pulltorefresh.library.PullToRefreshBase;

import butterknife.ButterKnife;



/**
 * Created by wangfan on 2016/12/31.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener ,
        DataCallBack,PullToRefreshBase.OnRefreshListener2,CommonDialog.DialogClickListener{
    private LinearLayout contentLayout;
    private LinearLayout.LayoutParams layoutParams;
    private ImageView title_back;
    private TextView tvLeft;
    private TextView title_option;
    private ImageView imRight;
    private TextView tvRead;
    private TextView title;
    private ImageView imgTitle;
    private RelativeLayout title_bar;
    private LayoutInflater layoutInflater;
    private int layoutId;
    private Context context;
    private Activity activity;
    public PreferencesHelper preferencesHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        init();
    }

    private void init() {
        context = this;
        activity = this;
        ActivityGroup.addActivity(this);
        preferencesHelper = PreferencesHelper.get(this);
        contentLayout = (LinearLayout) findViewById(R.id.content_layout);
        layoutInflater = LayoutInflater.from(this);
        layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        initTitleBar();
        contentLayout.addView(layoutInflater.inflate(getLayout(), null), layoutParams);
        ButterKnife.bind(this);
        initView();
        initData();
        initListener();
    }

    public void initTitleBar() {
        title_bar = (RelativeLayout) contentLayout.findViewById(R.id.title_bar);
        title_back = (ImageView) findViewById(R.id.title_back);
        title_option = (TextView) findViewById(R.id.title_option);
        title = (TextView) findViewById(R.id.title_text);
        imgTitle = (ImageView) findViewById(R.id.image_title);
        tvLeft = (TextView) findViewById(R.id.title_left);
        imRight = (ImageView) findViewById(R.id.image_right);
        tvRead = (TextView) findViewById(R.id.text_unread_massage);
        title.setOnClickListener(this);
        title_back.setOnClickListener(this);
        title_option.setOnClickListener(this);
        imRight.setOnClickListener(this);
    }

    @Override
    public void setTitle(int resId) {
        title.setText(getString(resId));
    }

    public void setTitle(String titleTx) {
        showTitleBar();
        hideImageRight();
        title_option.setVisibility(View.INVISIBLE);
        title.setText(titleTx);
        imgTitle.setVisibility(View.GONE);
        title_back.setVisibility(View.VISIBLE);
    }

    public void setTitleNotBack(String titleTx) {
        showTitleBar();
        hideImageRight();
        title_option.setVisibility(View.INVISIBLE);
        title.setText(titleTx);
        imgTitle.setVisibility(View.GONE);
        title_back.setVisibility(View.INVISIBLE);
    }

    public void hideRead() {
        tvRead.setVisibility(View.GONE);
    }

    public void showRead() {
        tvRead.setVisibility(View.VISIBLE);
    }


    public void showTitleBar() {
        if (title_bar.getVisibility() != View.VISIBLE) {
            title_bar.setVisibility(View.VISIBLE);
        }
    }

    public void hideTitleBar() {
        if (title_bar.getVisibility() != View.GONE) {
            title_bar.setVisibility(View.GONE);
        }
    }

    public void showImageTitle() {
        showTitleBar();
        hideImageRight();
        title_option.setVisibility(View.INVISIBLE);
        title.setVisibility(View.GONE);
        imgTitle.setVisibility(View.VISIBLE);
        title_back.setVisibility(View.VISIBLE);
    }

    public void showLeftText(String str) {
        title_back.setVisibility(View.GONE);
        tvLeft.setVisibility(View.VISIBLE);
        tvLeft.setOnClickListener(this);
    }

    public void showLeftText(String str, View.OnClickListener btClickListener) {
        title_back.setVisibility(View.GONE);
        tvLeft.setVisibility(View.VISIBLE);
        tvLeft.setText(str);
        tvLeft.setOnClickListener(btClickListener);
    }

    public void hideTitleBackBt() {
        title_back.setVisibility(View.INVISIBLE);
    }

    public void hideTitleRight() {
        title_option.setVisibility(View.INVISIBLE);
    }

    public void hideImageRight() {
        imRight.setVisibility(View.GONE);
    }

    public void showTitleRightBt(int resId) {
        title_option.setText(getString(resId));
        title_option.setVisibility(View.VISIBLE);
    }

    public void showTitleRightBt(String btText, View.OnClickListener btClickListener) {
        title_option.setText(btText);
        title_option.setVisibility(View.VISIBLE);
        imRight.setVisibility(View.GONE);
        title_option.setOnClickListener(btClickListener);
    }

    public void showImageRight(int icon, View.OnClickListener btClickListener) {
        title_option.setVisibility(View.GONE);
        imRight.setImageDrawable(getContext().getResources().getDrawable(icon));
        imRight.setVisibility(View.VISIBLE);
        imRight.setOnClickListener(btClickListener);
    }

    public void showImageRight(int icon) {
        title_option.setVisibility(View.GONE);
        imRight.setImageDrawable(getContext().getResources().getDrawable(icon));
        imRight.setVisibility(View.VISIBLE);
    }

    public void setContentLayout(int layoutId) {
        this.layoutId = layoutId;
    }

    public void showInfo(String info) {
        Tools.ShowInfo(getContext(), info);
    }

    public void startNewActivity(Class<?> cls) {
        startActivity(new Intent(this, cls));
    }

    public void startNewActivity(Class<?> cls, String data) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        startActivity(intent);
    }

    public void startNewActivityWithData(Class<?> cls, String data) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        startActivity(intent);
    }

    public void startNewActivityWithDataAndTitle(Class<?> cls, String data, String title) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        intent.putExtra("title", title);
        startActivity(intent);
    }

    public void startNewActivityWithData(Class<?> cls, String data, int requestCode) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        startActivityForResult(intent, requestCode);
    }

    public void startNewActivityWithData(Class<?> cls, String data, String value, int requestCode) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        intent.putExtra("value", value);
        startActivityForResult(intent, requestCode);
    }

    public void startNewActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(new Intent(this, cls), requestCode);
    }

    public void startNewActivityForResult(Class<?> cls) {
        startActivityForResult(new Intent(this, cls), 100);
    }

    public void startNewActivityForResultWithData(Class<?> cls, String data, int requestCode) {
        Intent intent = new Intent(this, cls);
        intent.putExtra("data", data);
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back:
                finish();
                break;
        }
    }

    protected abstract int getLayout();

    public abstract void initView();

    public abstract void initData();

    public abstract void initListener();

    public Context getContext() {
        return context;
    }

    public Activity getActivity() {
        return activity;
    }


    /**
     * 网络请求的回调接口实现
     *
     * @param result
     */

    @Override
    public void solve(String result) {

    }

    @Override
    public void dataBack(Object data, int order) {

    }

    @Override
    public void errorBack(String error) {

    }

    @Override
    public void finishBack() {

    }

    @Override
    public void upDateApp(Object data) {

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void dialogSure(String data) {

    }

    @Override
    public void dialogCancel() {

    }


}
